<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: ANSI C library interface</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_addon_clib.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">ANSI C library interface </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p><b>Path:</b> /sdk/add_on/clib/</p>
<p>This add-on defines a pure C interface, that can be used in those applications that do not understand C++ code but do understand C, e.g. Delphi, Java, and D.</p>
<p>To compile the AngelScript C library, you need to compile the library source files in sdk/angelscript/source together with the source files in sdk/add-on/clib, and link them as a shared dynamic library. In the application that will use the AngelScript C library, you'll include the <code>angelscript_c.h</code> header file, instead of the ordinary <code>angelscript.h</code> header file. After that you can use the library much the same way that it's used in C++.</p>
<p>To find the name of the C functions to call, you normally take the corresponding interface method and give a prefix according to the following table:</p>
<table  border="0" cellspacing="0" cellpadding="0">
<tr>
<td><b>interface &#160;</b></td><td><b>prefix&#160;</b> </td></tr>
<tr>
<td><a class="el" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> &#160; </td><td>asEngine_ </td></tr>
<tr>
<td><a class="el" href="classas_i_script_module.html" title="The interface to the script modules.">asIScriptModule</a> &#160; </td><td>asModule_ </td></tr>
<tr>
<td><a class="el" href="classas_i_script_context.html" title="The interface to the virtual machine.">asIScriptContext</a> &#160; </td><td>asContext_ </td></tr>
<tr>
<td><a class="el" href="classas_i_script_generic.html" title="The interface for the generic calling convention.">asIScriptGeneric</a> &#160; </td><td>asGeneric_ </td></tr>
<tr>
<td><a class="el" href="classas_i_script_object.html" title="The interface for an instance of a script object.">asIScriptObject</a> &#160; </td><td>asObject_ </td></tr>
<tr>
<td><a class="el" href="classas_i_object_type.html" title="The interface for an object type.">asIObjectType</a> &#160; </td><td>asObjectType_ </td></tr>
<tr>
<td><a class="el" href="classas_i_script_function.html" title="The interface for a script function description.">asIScriptFunction</a> &#160; </td><td>asFunction_ </td></tr>
</table>
<p>All interface methods take the interface pointer as the first parameter when in the C function format, the rest of the parameters are the same as in the C++ interface. There are a few exceptions though, e.g. all parameters that take an <code><a class="el" href="structas_s_func_ptr.html" title="Represents a function or method pointer.">asSFuncPtr</a></code> take a normal function pointer in the C function format.</p>
<p>Example:</p>
<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;stdio.h&gt;</span>
<span class="preprocessor">#include &lt;assert.h&gt;</span>
<span class="preprocessor">#include &quot;angelscript_c.h&quot;</span>

<span class="keywordtype">void</span> MessageCallback(<a class="code" href="structas_s_message_info.html" title="Represents a compiler message.">asSMessageInfo</a> *msg, <span class="keywordtype">void</span> *);
<span class="keywordtype">void</span> PrintSomething();

<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)
{
  <span class="keywordtype">int</span> r = 0;

  <span class="comment">// Create and initialize the script engine</span>
  <a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine = <a class="code" href="angelscript_8h.html#af84c6359750675bf3ceccf373286a533" title="Creates the script engine.">asCreateScriptEngine</a>(<a class="code" href="angelscript_8h.html#a99c6b8b0882e45e5d0b2ed19f6f7a157">ANGELSCRIPT_VERSION</a>);
  r = asEngine_SetMessageCallback(engine, (asFUNCTION_t)MessageCallback, 0, <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4" title="A cdecl function.">asCALL_CDECL</a>); assert( r &gt;= 0 );
  r = asEngine_RegisterGlobalFunction(engine, <span class="stringliteral">&quot;void print()&quot;</span>, (asFUNCTION_t)PrintSomething, <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4a68ae43cc91cdfc3fa4590c9e6164e4f4" title="A cdecl function.">asCALL_CDECL</a>); assert( r &gt;= 0 );

  <span class="comment">// Execute a simple script</span>
  r = asEngine_ExecuteString(engine, 0, <span class="stringliteral">&quot;print()&quot;</span>, 0, 0);
  <span class="keywordflow">if</span>( r != <a class="code" href="angelscript_8h.html#a867f14b4137dd4602fda1e616b217a69a6d3730dd7a91aff81cafaaca4e93efaa" title="The context has successfully completed the execution.">asEXECUTION_FINISHED</a> )
  {
      printf(<span class="stringliteral">&quot;Something wen&#39;t wrong with the execution\n&quot;</span>);
  }
  <span class="keywordflow">else</span>
  {
      printf(<span class="stringliteral">&quot;The script was executed successfully\n&quot;</span>);
  }

  <span class="comment">// Release the script engine</span>
  asEngine_Release(engine);
  
  <span class="keywordflow">return</span> r;
}

<span class="keywordtype">void</span> MessageCallback(<a class="code" href="structas_s_message_info.html" title="Represents a compiler message.">asSMessageInfo</a> *msg, <span class="keywordtype">void</span> *)
{
  <span class="keyword">const</span> <span class="keywordtype">char</span> *msgType = 0;
  <span class="keywordflow">if</span>( msg-&gt;<a class="code" href="structas_s_message_info.html#a6aa9231534b8aea2a3099cdc3206bcc8" title="The type of message.">type</a> == 0 ) msgType = <span class="stringliteral">&quot;Error  &quot;</span>;
  <span class="keywordflow">if</span>( msg-&gt;<a class="code" href="structas_s_message_info.html#a6aa9231534b8aea2a3099cdc3206bcc8" title="The type of message.">type</a> == 1 ) msgType = <span class="stringliteral">&quot;Warning&quot;</span>;
  <span class="keywordflow">if</span>( msg-&gt;<a class="code" href="structas_s_message_info.html#a6aa9231534b8aea2a3099cdc3206bcc8" title="The type of message.">type</a> == 2 ) msgType = <span class="stringliteral">&quot;Info   &quot;</span>;

  printf(<span class="stringliteral">&quot;%s (%d, %d) : %s : %s\n&quot;</span>, msg-&gt;<a class="code" href="structas_s_message_info.html#aeca6368be12c84b62ed8c659b1e4615c" title="The script section where the message is raised.">section</a>, msg-&gt;<a class="code" href="structas_s_message_info.html#a21ef80321436f229a547411a6598ea21" title="The row number.">row</a>, msg-&gt;<a class="code" href="structas_s_message_info.html#a08b23a360ac52110323bbf4aad553d9d" title="The column.">col</a>, msgType, msg-&gt;<a class="code" href="structas_s_message_info.html#af76694c6342dd82ef6aca0dff42072f5" title="The message text.">message</a>);
}

<span class="keywordtype">void</span> PrintSomething()
{
  printf(<span class="stringliteral">&quot;Called from the script\n&quot;</span>);
}
</pre></div> </div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:03 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
